using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Threading;

namespace BinocularVision
{
    public class Body
    {
        #region Fields
        private WebCam leftWebCam;
        private WebCam rightWebCam;
        private Thread sensorPollingThread;
        private double sensorPollingThreadSleepInterval;
        private bool running;
        #endregion

        #region Constructors
        public Body()
        {
            leftWebCam = new WebCam();
            rightWebCam = new WebCam();
        }        
        #endregion

        #region Private methods
        private void SensorPollingLoop()
        {
            while (running)
            {
                Robot.Instance.Body.LeftWebCam.UpdateReading();
                Robot.Instance.Body.RightWebCam.UpdateReading();
                Thread.Sleep((int)(sensorPollingThreadSleepInterval*1000));
            }
        }

        #endregion
        
        #region Public methods
        public void Initialize()
        {
            leftWebCam.Initialize();
            rightWebCam.Initialize();
        }

        public void Start()
        {
            leftWebCam.Start();
            rightWebCam.Start();
            running = true;
            sensorPollingThread = new Thread(SensorPollingLoop);
            sensorPollingThread.Start();
        }

        public void Stop()
        {
            running = false;
            sensorPollingThread.Join();
        }

        public void Disconnect()
        {
            leftWebCam.Disconnect();
            rightWebCam.Disconnect();
        }
        #endregion

        #region Properties
        public WebCam LeftWebCam
        {
            get { return leftWebCam; }
        }

        public WebCam RightWebCam
        {
            get { return rightWebCam; }
        }
        public double SensorPollingThreadSleepInterval
        {
            get { return sensorPollingThreadSleepInterval; }
            set { sensorPollingThreadSleepInterval = value; }
        }

        #endregion
    }
}
